#!/usr/bin/env python
# -*- coding: utf-8 -*-

from cv_bridge import CvBridge, CvBridgeError

bridge = CvBridge()


def ros_to_cv2(img_ros, encoding='passthrough'):
    """
    使用cv_bridge将ROS图像数据转换成OpenCV图像数据
    """
    try:
        img_cv2 = bridge.imgmsg_to_cv2(img_ros, encoding)
    except CvBridgeError as e:
        print('error:', repr(e))

    return img_cv2


def cv2_to_ros(img_cv2, encoding='passthrough'):
    """
    使用cv_bridge将OpenCV图像数据转换成ROS图像数据
    """
    try:
        img_ros = bridge.cv2_to_imgmsg(img_cv2, encoding)
    except CvBridgeError as e:
        print('error:', repr(e))

    return img_ros
